const tagArray = ["理想主义青年", "双重人格的", "颜控", "义气虾", "颜控", "的", "牛皮的", "吊炸天的", "mmp的", "理想主义者", "牛皮的", "吊炸天的", "mmp的", "理想主义者", "牛皮的", "吊炸天的", "mmp的", "理想主义者", "牛皮的", "的", "吊炸天的", "mmp的", "牛皮的", "吊炸天的", "mmp的", "理想主义者", "牛皮的", "吊炸天的", "mmp的", "理想主义者", "牛皮的", "吊炸天的", "mmp的", "的", "理想主义者", "牛皮的", "吊炸天的", "mmp的", "理想主义者", "牛皮的", "吊炸天的", "mmp的", "理想主义者", "牛皮的", "吊炸天的", "mmp的", "的","牛皮的", "吊炸天的", "mmp的"]
var util = require('../../../../utils/util.js')
var _this
Page({
    data: {
      dataSource: [],
      customDataSource: [],
      styleArray:[],
      customStyleArray: [],
      tagSelectedArray: [],  //存放id
      customTagSelectedArray: [] //存放标签文字
    },
    tagDidTap: function(e){
      var tapIndex = e.target.dataset.index
      var tapContent = _this.data.dataSource[tapIndex]
      var index = _this.data.tagSelectedArray.indexOf(tapContent.id)
      if (index == -1){
        _this.data.tagSelectedArray.push(tapContent.id)
        _this.data.styleArray[tapIndex] = 'itemSelected'
      }else{
       _this.data.tagSelectedArray.splice(index,1)
        _this.data.styleArray[tapIndex] = 'itemNormal'
      }
      _this.setData({
        tagSelectedArray: _this.data.tagSelectedArray,
        styleArray: _this.data.styleArray
      })
      console.log(_this.data.tagSelectedArray)
    },
    customtagDidTap: function (e) {
      var tapIndex = e.target.dataset.index
      var tapContent = _this.data.customDataSource[tapIndex]
      var index = _this.data.customTagSelectedArray.indexOf(tapContent)
      if (index == -1) {
        _this.data.customTagSelectedArray.push(tapContent)
        _this.data.customStyleArray[tapIndex] = 'itemSelected'
      } else {
        _this.data.customTagSelectedArray.splice(index, 1)
        _this.data.customStyleArray[tapIndex] = 'itemNormal'
      }
      _this.setData({
        customTagSelectedArray: _this.data.customTagSelectedArray,
        customStyleArray: _this.data.customStyleArray
      })
      console.log(_this.data.customTagSelectedArray)
    },
    onLoad: function() {
       _this = this
       this.refreshData()
      util.getKeyWord(_this.getkeyWordDidResponse, _this.requestDidFaild)
       console.log(_this.data.styleArray,_this.data.tagSelectedArray)
    },
    handleUpdate: () => {
      var data = {}
      data.labels = _this.data.tagSelectedArray.join()
      if (_this.data.customTagSelectedArray.length > 0) {
        data.otherLabels = _this.data.customTagSelectedArray.join()
      }
      util.updateUserInfo(_this.requestDidResponse, _this.requestDidFaild, data)
    },
    getkeyWordDidResponse: (res) => {
      console.log('did receive response tag: ' + res)
      if (res.status == '200') {
        _this.setData({
          dataSource: res.data.result
        })
      }
    },
    requestDidResponse: (res) => {
      if (res.status == '200') {
      }
    },
    requestDidFaild: (res) => {

    },
    customButtonSelected: (e) => {
      wx.navigateTo({
        url: '../infoTagCustom/infoTagCustom',
      })
    },
    addTag: (tag) => {
      console.log(tag)
      var index = _this.data.customDataSource.indexOf(tag)
      if (index == -1) {
        _this.data.customDataSource.push(tag)
        _this.data.customTagSelectedArray.push(tag)
        _this.data.customStyleArray.push('itemSelected')
        _this.setData({
          customDataSource: _this.data.customDataSource,
          customStyleArray: _this.data.customStyleArray
        })
        return true
      } else {
        return false
      }
    },
    nextButtonSelected: (e) => {
      if (_this.data.tagSelectedArray.length == 0) {
        wx.showToast({
          title: '请选择至少一个非自定义标签',
          icon: 'none'
        })
        return
      }
      _this.handleUpdate()
      wx.navigateTo({
        url: '../infoAvatar/infoAvatar',
      })
    },
    refreshData: function() {
      //根据dataSource及用户已经选择标签得到标签样式数组初始值
       let newStyleArray =  _this.data.dataSource.map(
         (element) => {
            var result = _this.data.tagSelectedArray.find(
               (value) => {
                 return value == element.id
               }
             )
           if (result == undefined){
             return 'itemNormal'
           }else{
             return 'itemSelected'
           }
         }
       )
       _this.setData({
         styleArray: newStyleArray
       })
    },
  refreshCustomData: function () {
    //根据dataSource及用户已经选择标签得到标签样式数组初始值
    let newStyleArray = _this.data.customDataSource.map(
      (element) => {
        var result = _this.data.customTagSelectedArray.find(
          (value) => {
            return value == element
          }
        )
        if (result == undefined) {
          return 'itemNormal'
        } else {
          return 'itemSelected'
        }
      }
    )
    _this.setData({
      customStyleArray: newStyleArray
    })
  }
})